package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.ItemImage;
import com.armani.model.Result;
import com.armani.service.ImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ImgController
 * @Description 商品图片控制层
 * @Author Admin
 * @Date 2021/11/27 17:51
 * @Version 1.0
 **/
@Controller
@RequestMapping("/img")
public class ImgController {
    //自动装配

    @Autowired
    private ImgService imgService;
    @GetMapping(value ="/listSearch.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String pageSearch(@RequestParam Integer page,
                              @RequestParam Integer limit,
                              @RequestParam(required = false) Integer id){
        Integer startRow=(page-1)*limit;
        List<ItemImage> imgs =imgService.pageSearch(startRow, limit,id);
        Integer imgCount=imgService.getCount(id);
        Result result=new Result();
        result.setCount(imgCount);
        result.setData(imgs);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping(value ="/insert.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String insert( @RequestParam String image,
                           @RequestParam String descrip,
                           @RequestParam Integer skuId,
                           @RequestParam Integer sort,
                           @RequestParam Integer master,
                           @RequestParam Integer state,
                           @RequestParam Date lastTime){
        Integer insert=imgService.insert(image, descrip, skuId, sort, master, state, lastTime);
        Result result=new Result();
        result.setData(insert);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping(value ="/update.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String update(@RequestParam Integer id,
                          @RequestParam String image,
                          @RequestParam String descrip,
                          @RequestParam Integer skuId,
                          @RequestParam Integer sort,
                          @RequestParam Integer master,
                          @RequestParam Integer state,
                          @RequestParam Date lastTime){
        Integer update=imgService.update(id,image, descrip, skuId, sort, master, state, lastTime);
        Result result=new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping(value ="/delete.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String delete(@RequestParam Integer id){
        Integer update=imgService.delete(id);
        Result result=new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }
}
